<!--
 * @FileDescription: 该文件的描述信息
 * @Author: 作者信息
 * @Date: 文件创建时间
 * @LastEditors: 最后更新作者
 * @LastEditTime: 最后更新时间
 -->
<template>
  <el-form
    :model="passwordForm"
    role-icon
    :rules="rules"
    ref="passwordForm"
    class="password_Form"
    label-width="80px"
  >
    <h1>修改密码</h1>
    <el-form-item prop="oldPassword" label="旧密码:">
      <el-input
        type="password"
        v-model="passwordForm.oldPassword"
        autocomplete="off"
        placeholder="请输入旧密码"
      ></el-input>
    </el-form-item>
    <el-form-item prop="password" label="新密码:">
      <el-input
        type="password"
        v-model="passwordForm.password"
        autocomplete="off"
        placeholder="请输入新密码"
      ></el-input>
    </el-form-item>
    <el-form-item prop="okPassword" label="确认密码:">
      <el-input
        type="password"
        v-model="passwordForm.okPassword"
        autocomplete="off"
        placeholder="请输入确认密码"
      ></el-input>
    </el-form-item>
    <el-button type="primary" id="save" @click="save">保存</el-button>
  </el-form>
</template>
<script>
export default {
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入旧密码"));
      }
      else {
        callback();
      }
    };
    //验证密码
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.passwordForm.okPassowrd !== "") {
          this.$refs.passwordForm.validateField("okPassowrd");
        }
        callback();
      }
    };
    //验证确认密码
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入确认密码"));
      } else if (value !== this.passwordForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else if(value == this.passwordForm.oldPassword){
         callback(new Error("新密码不能与旧密码相同!"));
      } 
      else {
        callback();
      }
    };
    return {
      passwordForm: {
        oldPassword: "",
        password: "",
        okPassword: "",
      },
      rules: {
        oldPassword: [{ validator: validateOldPass, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        okPassword: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    save() {
      let _that = this
      this.$refs['passwordForm'].validate((valid) => {
        if (valid) {
            _that.changePasswordAxios();
        }
      });
    },
    changePasswordAxios(){
      let _that = this
        this.$api.teacher
        .changePassword({password:this.passwordForm.oldPassword,newPassword:this.passwordForm.password})
        .then(function(response){
          console.log(response);
          let {data,code,message}=response.data
          console.log(code);
          if(code == 500){
            _that.$message({
              showClose: true,
              message: message,
              type: "error",
            });
          }else{
             _that.$message({
              showClose: true,
              message: message,
              type: "success",
            });
          }
        }).catch(function (error) {
          console.log(error);
        });
    }
  },
};
</script>
<style scoped>
.password_Form {
  width: 300px;
}
h1 {
  padding-left: 50px;
  text-align: center;
}
#save{
  margin-left: 30px;
}
.password_Form,#save {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
</style>